/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.configuration;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GameConfigurationManager {
	private final static Logger LOG = LoggerFactory.getLogger(GameConfigurationManager.class);

	private final static String CONFIGURATION_FILE_PATH = "config.xml";

	private static final GameConfiguration defaultConfiguration;

	private static GameConfiguration gameConfiguration = null;

	private static boolean cached = false;

	static {
		defaultConfiguration = new GameConfiguration();
		defaultConfiguration.setLanguage("en");
		try {
			defaultConfiguration.setPlayerName(InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException e) {
			LOG.info(e.getMessage(), e);
			defaultConfiguration.setPlayerName("Unknown");
		}

		gameConfiguration = defaultConfiguration;
	}

	public static GameConfiguration getConfiguration() {
		if (cached) {
			return gameConfiguration;
		}

		File file = new File(CONFIGURATION_FILE_PATH);
		if (!file.exists()) {
			LOG.info("Configuration file not found, using default config");
			return defaultConfiguration;
		}

		try {
			JAXBContext context = JAXBContext.newInstance(GameConfiguration.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			LOG.info("Trying to read configuration from file");
			GameConfiguration configuration = (GameConfiguration) unmarshaller.unmarshal(file);
			LOG.info("Successfully read configuration from file");
			gameConfiguration = configuration;
			cached = true;
			return configuration;
		} catch (Exception ex) {
			LOG.error("Error during configuration file read, using default config", ex);
			return defaultConfiguration;
		}
	}

	public static void saveConfiguration(GameConfiguration configuration) {
		LOG.info("Trying to save configuration in file");

		try {
			File configurationFile = new File(CONFIGURATION_FILE_PATH);
			if (!configurationFile.exists()) {
				LOG.info("Creating new configuration file");
				configurationFile.createNewFile();
			}

			JAXBContext context = JAXBContext.newInstance(GameConfiguration.class);
			Marshaller marshaller = context.createMarshaller();
			marshaller.marshal(configuration, configurationFile);
			LOG.info("Successfully saved configuration in file");
			gameConfiguration = configuration;

		} catch (Exception ex) {
			LOG.error("Error while saving configuration file", ex);
		}
	}
}
